package com.briup.corejava.day22;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * lambda表达式练习
 * 比较器
 * Thread
 * Callable
 */
public class Test7 {
    public static void main(String[] args) {
        // 以下全部使用lambda
        // 1、提供treeSet，使用String来进行排序
        // Comparator 两个参数，一个返回值
        Set<String> set = new TreeSet<>((a,b)->b.compareTo(a));
        // 2、新建子线程t1，实现Runnable，循环输出1-10
        Thread t1 = new Thread(()-> {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        );
        t1.start();
        // 3、新建子线程t2，实现Callable，计算1-10的累加和
        FutureTask<Integer> futureTask = new FutureTask<>(()->{
            int sum = 0;
            for (int i = 0; i < 10; i++) {
                sum += i;
            }
            return sum;
        });
        Thread t2 = new Thread(futureTask);
        t2.start();
        try {
            System.out.println("线程t2的计算结果："+futureTask.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

    }
}
